package org.dolphin.base.data.provider;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import javax.sql.DataSource;
import java.sql.Connection;

import org.javawing.component.cache.Cache;
import org.javawing.component.cache.CacheFactory;
import org.javawing.component.util.ClassUtils;
//import org.apache.oro.util.CacheLRU;

public class JDBCFactory {
	private static DataSource ds;

	private JDBCFactory() {
	}
	

	static {
		Properties props = new Properties();
		try {
			props.load(JDBCFactory.class.getResourceAsStream(
							"datasource-config.properties"));
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		String Class = props.getProperty("class");
		String url = props.getProperty("serverURL");
		String driver = props.getProperty("driver");
		String user = props.getProperty("username");
		String password = props.getProperty("pasword");
		int minCon = Integer.parseInt(props.getProperty("minCon"));
		int maxCon = Integer.parseInt(props.getProperty("maxCon"));
		double conTimeout = Double.parseDouble(props.getProperty("conTimeout"));
		
		Constructor cotr = null;
		try {
			cotr = ClassUtils.forName(Class).getConstructor(
					new Class[] { String.class, String.class, String.class,
							String.class, int.class, int.class, double.class });
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object dataSource = null;
		try {
			dataSource = cotr.newInstance(new Object[] { driver, url, user,
					password, new Integer(minCon), new Integer(maxCon),
					new Double(conTimeout) });
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ds = (DataSource) dataSource;
		
	}

	public static DataSource DataSource() {
		return ds;
	}
	
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Connection getConnection(String user,String pwd) {
		try {
			return ds.getConnection(user,pwd);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}

	public static Cache getDaoCache() {
		String CacheName = "DaoCache";
		String CacheClass = "org.javawing.component.cache.LruCache";
		int CacheSize = 1024;
		int CacheLife = 10000;
		Cache daoCache = CacheFactory.createCache(CacheName, CacheClass,
				CacheSize, CacheLife);
		return daoCache;
	}

}
